﻿using System;
using System.Text;
using System.Data;
using System.Web.UI;

using BLL;
using Model;
using Utility;

namespace EvaluationSystem
{
    public partial class TesterInfoInput : System.Web.UI.Page
    {
        private TesterInfoBLL testerInfobll = new TesterInfoBLL();
        private ActivityBLL activitybll = new ActivityBLL();
        private GuidBLL guidbll = new GuidBLL();
        private EnterpriseUserBLL enterpriseUserbll = new EnterpriseUserBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["logopath"] != null)
                {
                    if (string.IsNullOrEmpty(Request.QueryString["logopath"]))
                    {
                        usrLogo.ImageUrl = "~/images/5050.jpg";
                        usrLogo.Visible = false;                        
                    }
                    else
                    {
                        usrLogo.ImageUrl = Request.QueryString["logopath"].ToString().Replace("\\", "/");
                    }
                }

                if (Session["isTesting"] == null)//如果没有输入序列号，则禁止访问此页面
                {
                    Response.Redirect("Tlogin.aspx");
                }

                txtbUserName.Focus();
                btnChange.Attributes.Add("onclick", "return Check();");
            }
        }

        protected void btnChange_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (Session["isTesting"] == null)//如果没有输入序列号，则禁止访问此页面
                {
                    Response.Redirect("Tlogin.aspx");
                }

                TesterInfo testerInfo = new TesterInfo();
                int activityId = guidbll.GetActivityId(Request["guid"].ToString());
                DataSet dsEnterInfo = activitybll.GetEUsrInfoByActivityId(activityId);

                if (dsEnterInfo != null)
                {
                    testerInfo.Tester_enterpriseid = int.Parse(dsEnterInfo.Tables[0].Rows[0]["activity_enterpriseuid"].ToString());
                    testerInfo.Tester_enterprise = dsEnterInfo.Tables[0].Rows[0]["users_incname"].ToString();
                    enterpriseUserbll.UpdateFirstTimeLogon(testerInfo.Tester_enterpriseid.ToString(), DateTime.Now);
                }
                if (Request["guid"] != null)
                {
                    testerInfo.Tester_sn = Request["guid"].ToString();
                    testerInfo.Tester_name = txtbUserName.Text.Trim();
                    testerInfo.Tester_sex = ddlSex.SelectedValue;
                    testerInfo.Tester_mobile = txtbMobile.Text.Trim();
                    testerInfo.Tester_experience = ddlExperience.SelectedValue;
                    testerInfo.Tester_experienceinner = ddlExperienceInner.SelectedValue;
                    testerInfo.Tester_email = txtbEmail.Text.Trim();
                    testerInfo.Tester_department = txtbDepartment.Text.Trim();
                    testerInfo.Tester_education = ddlEducation.SelectedValue;
                    testerInfo.Tester_dealer = txtbDealer.Text.Trim();
                    testerInfo.Tester_birthdate = DateTime.Parse(txtBirthday.Value);
                    testerInfo.Tester_application = txtbApplication.Text.Trim();

                    testerInfobll.Add(testerInfo);

                    Response.Redirect("QuestionRepeat.aspx?guid=" + Request["guid"].ToString() +
                      "&logopath=" + Request.QueryString["logopath"].Replace("\\","/"));

                   /*
                    string _guid = Request["guid"].ToString();
                    string _logopath = Request.QueryString["logopath"].ToString().Replace("\\","/");

                    StringBuilder sb = new StringBuilder();
                    sb.Append("<script language='javascript'>");
                    sb.Append("ShowDlg('" + _guid + "'," + "'" + _logopath + "')");
                    sb.Append("</script>");
                    ClientScript.RegisterStartupScript(GetType(), "", sb.ToString());
                    */
                }
                else
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.ShowAndRedirect(this, ex.Message, "Tlogin.aspx");
            }
        }
    }
}